package com.xys.auth.controller;

import com.github.pagehelper.PageInfo;
import com.xys.auth.bean.Member;
import com.xys.auth.bean.Orders;
import com.xys.auth.bean.Product;
import com.xys.auth.bean.Traveller;
import com.xys.auth.mapper.ProductMapper;
import com.xys.auth.service.IOrderService;
import com.xys.auth.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.ModalityListener;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    IOrderService orderService;

    @Autowired
    private IProductService productService;



    @PreAuthorize("hasAuthority('/orders/findAll.do')")
    @RequestMapping("findAll.do")
    public ModelAndView findAll(
            @RequestParam(name = "page",required = true,defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") Integer pageSize
    ){

        List<Orders> ordersList = orderService.findAll(page, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        PageInfo pageInfo = new PageInfo(ordersList);

        modelAndView.setViewName("orders-list");

        modelAndView.addObject("pageInfo",pageInfo);
        return modelAndView;
    }

    @PreAuthorize("hasAuthority('/orders/findById.do')")
    @RequestMapping("/findById.do")
    public ModelAndView findById(String id){
        Orders orders = orderService.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("orders-show");
        modelAndView.addObject("orders",orders);
        return modelAndView;
    }

    //--------------------------------------------

    @RequestMapping("findRoleByIdAndAllProduct.do")
    public ModelAndView findRoleByIdAndAllProduct(String  id){

        List<Product> products = productService.findAll();

        Orders orders=new Orders();
        orders.setId(id);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("orders-product-add");
        modelAndView.addObject("productList",products);

        modelAndView.addObject("orders",orders);
        return modelAndView;

    }


    @RequestMapping("findOrdersByIdAndAllTraveller.do")
    public ModelAndView findOrdersByIdAndAllTraveller(String  id){


        Orders orders=new Orders();
        orders.setId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("traveller-add");
        modelAndView.addObject("orders",orders);
        return modelAndView;

    }




    @RequestMapping("addProductToOrders.do")
    public String addProductToOrders(String id, String[] ids)
    {
        orderService.addProductToOrders(id, ids);
//更新支付情况
        orderService.update(id);
        return "redirect:findAll.do";
    }




    @RequestMapping("/save.do")
    public String save(Orders orders, Traveller traveller, Member member){
        //调用service方法，执行业务逻辑
        orderService.save(orders);//建立空订单
        orderService.saveMember(member,orders);
        //整理返回值，返回相应的页面
        return "redirect:findAll.do";
    }


    @RequestMapping("/saveTraveller.do")
    public String saveTraveller(String  id, Traveller traveller){
        //调用service方法，执行业务逻辑
        orderService.saveTraveller(traveller,id);
        //整理返回值，返回相应的页面
        return "redirect:findAll.do";
    }

    @RequestMapping("/delete.do")
    public String delete(String[] ids)
    {

        for (int i = 0; i < ids.length ; i++) {
            orderService.delete(ids[i]);
        }
        return "redirect:findAll.do";
    }

}
